//
//  MainTabBarController.m
//  YAMainProject
//
//  Created by 郑逸凯 on 2018/9/20.
//  Copyright © 2018年 郑逸凯. All rights reserved.
//

#import "MainTabBarController.h"
#import "MyTabBar.h"
#import <YASDK/JXTAlertController.h>
#import "BoatListViewController.h"
#import "ShipRouteViewController.h"
#import "PhotoAlbumViewController.h"
#import "BaseNavigationController.h"

@interface MainTabBarController ()<UITabBarControllerDelegate>

@end

@implementation MainTabBarController

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    ShipRouteViewController *shipRouteVC = [[ShipRouteViewController alloc] init];
//    homeVC.title = @"首页";
    
    
    BoatListViewController *boatListVC = [[BoatListViewController alloc] init];
    
    
    PhotoAlbumViewController *photoAlbumVC = [[PhotoAlbumViewController alloc] init];
    
   
    
 
    [self addChildController:shipRouteVC title:@"船队状态" imageName:@"tab_boat" selectedImageName:@"tab_boat_sel" navVc:[BaseNavigationController class]];
    
    [self addChildController:boatListVC title:@"监控截图" imageName:@"tab_monitor" selectedImageName:@"tab_monitor_sel" navVc:[BaseNavigationController class]];
    
    [self addChildController:photoAlbumVC title:@"相册" imageName:@"tab_album" selectedImageName:@"tab_album_sel" navVc:[BaseNavigationController class]];
    
    
    
    [[UITabBar appearance] setBackgroundImage:[self imageWithColor:[UIColor whiteColor]]];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
    lineView.backgroundColor = RGB16(0xE6E6E6);
    [self.tabBar addSubview:lineView];
    //  设置tabbar
    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
    // 设置自定义的tabbar
//    [self setCustomtabbar];
    self.delegate = self;
    [self setSelectedIndex:1];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (BOOL)shouldAutorotate{
    return [self.selectedViewController shouldAutorotate];    //默认YES
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return [self.selectedViewController supportedInterfaceOrientations];
    
}

#pragma mark - Public Methods
#pragma mark - Responder Actions
//- (void)centerBtnClick:(UIButton *)btn{
//    [RouteTool presentToNavigationContoroller:PulishConfigViewController.class params:nil];
//
//
//}
#pragma mark - Delegates
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    NSInteger index = [tabBarController.childViewControllers indexOfObject:viewController];
    if (index == 0) {
        [YAToast showTouchableTipMessageInView:@"该功能暂未开放，请耐心等候"];
        return NO;
    }
    return YES;
}

#pragma mark - Private Methods
//- (void)setCustomtabbar{
//    
//    MyTabBar *tabbar = [[MyTabBar alloc]init];
//    
//    [self setValue:tabbar forKeyPath:@"tabBar"];
//    
//    [tabbar.centerBtn addTarget:self action:@selector(centerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    
//    
//}

- (void)addChildController:(UIViewController*)childController title:(NSString*)title imageName:(NSString*)imageName selectedImageName:(NSString*)selectedImageName navVc:(Class)navVc
{
    
    childController.title = title;
//    childController.tabBarItem.title = title;
    childController.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 设置一下选中tabbar文字颜色
    [childController.tabBarItem setTitleTextAttributes:@{ NSForegroundColorAttributeName : RGBToColor(42, 136, 250) }forState:UIControlStateSelected];
    
    BaseNavigationController* nav = [[navVc alloc] initWithRootViewController:childController];
    
    [self addChildViewController:nav];
}
#pragma mark - Setters and Getters
#pragma mark - Helper
- (UIImage *)imageWithColor:(UIColor *)color{
    // 一个像素
    CGRect rect = CGRectMake(0, 0, 1, 1);
    // 开启上下文
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    [color setFill];
    UIRectFill(rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end
